package com.sec.etech.org.controller;

import com.sec.etech.org.service.EtechResourceManagerImpl;
import org.openbpm.base.api.aop.annotion.CatchErr;
import org.openbpm.base.api.query.QueryOP;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.openbpm.base.core.util.JsonUtil;
import org.openbpm.base.core.util.StringUtil;
import org.openbpm.base.db.model.query.DefaultQueryFilter;
import org.openbpm.org.core.manager.SysResourceManager;
import org.openbpm.org.core.model.SysResource;
import org.openbpm.org.rest.controller.SysResourceController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/etech/sysResource")
public class EtechSysResourceController extends SysResourceController {

    @Autowired
    EtechResourceManagerImpl sysResourceManager;

    /**
     * 保存子系统资源信息
     *
     * @param sysResource
     * @throws Exception void
     * @throws
     */
    @RequestMapping("import")
    @CatchErr
    public ResultMsg<String> importSave(@RequestBody SysResource sysResource) throws Exception {
        String resultMsg = null;
        String id = sysResource.getId();
        SysResource existResource = getResourceByAlias(sysResource);
        if(existResource!=null){
            id = existResource.getId();
            sysResource.setId(id);
            if(existResource.getSn()!=null){
                sysResource.setSn(existResource.getSn());
            }
        }

        if (StringUtil.isEmpty(id)) {
            sysResourceManager.create(sysResource);
            resultMsg = "添加子系统资源成功";
        } else {
            sysResourceManager.update(sysResource);
            resultMsg = "更新子系统资源成功";
        }

        return getSuccessResult(sysResource.getId(), resultMsg);

    }

    private SysResource getResourceByAlias(SysResource sysResource){
        DefaultQueryFilter queryFilter = new DefaultQueryFilter();
        queryFilter.addFilter("system_id_", sysResource.getSystemId(), QueryOP.EQUAL);
        queryFilter.addFilter("alias_", sysResource.getAlias(), QueryOP.EQUAL);
        List<SysResource> resources = sysResourceManager.query(queryFilter);
        if(resources!=null && resources.size()>0){
            return resources.get(0);
        }
        return null;
    }

    /**
     * 保存移动节点
     *
     * @param sysResource
     * @throws Exception void
     * @throws
     */
    @RequestMapping("saveTree")
    @CatchErr
    public ResultMsg<String> saveTree(@RequestBody SysResource sysResource) throws Exception {
        String resultMsg = null;
        if(sysResource==null || sysResource.getChildren()==null || sysResource.getChildren().isEmpty()){
            return getWarnResult("sysResource have not children");
        }
        List<SysResource> children = sysResource.getChildren();
        for(int i=0; i<children.size(); i++){
            SysResource child = (SysResource) JsonUtil.parseObject(JsonUtil.toJSONString(children.get(i)), SysResource.class) ;
            child.setSn(i+1);
            sysResourceManager.update(child);
        }
        resultMsg = "移动节点成功";
        return getSuccessResult(sysResource.getId(), resultMsg);
    }


    /**
     * 保存或更新子节点
     *
     * @param sysResource
     * @throws Exception void
     * @throws
     */
    @RequestMapping("saveAndUpdateTree")
    @CatchErr
    public ResultMsg<String> saveAndUpdateTree(@RequestBody SysResource sysResource, HttpServletRequest request) throws Exception {
        String resultMsg = null;
        if(sysResource==null || sysResource.getChildren()==null || sysResource.getChildren().isEmpty()){
            return getWarnResult("sysResource have not children");
        }
        String code = request.getParameter("code");
        sysResourceManager.saveAndUpdateTree(sysResource, code);
        resultMsg = "保存或更新成功";
        return getSuccessResult(sysResource.getId(), resultMsg);
    }
}
